﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MayTinh
{
    public partial class MayTinh : Form
    {
        double so1, so2, kq;
        string trangthainhapso;
        string trangthainhapdau;
        string pheptoan;
        bool dauphay = false;
        //cho nhap so1
        //cho nhap so2
        //dang nhap so1
        //dang nhap so2
        //dang nhap phep toan

        public MayTinh()
        {
            InitializeComponent();            
            xoa();
        }
        void doitrangthai()
        {
            switch (trangthainhapso)
            { 
                case "cho nhap so1":
                    trangthainhapso="dang nhap so1";
                    break;
                case "cho nhap so2":
                    trangthainhapso="dang nhap so2";
                    break;
                case "dang nhap so1":
                    trangthainhapso = "cho nhap so2";
                    break;
            }
        }
        void xoa()
        {
            txtKQ.Text = "0";
            so1 = so2 = double.NaN;
            kq = 0;
            trangthainhapdau = "";
            trangthainhapso = "cho nhap so1";
            pheptoan = "";
            dauphay = false;
        }
        void tinhkq()
        {
            switch (pheptoan)
            { 
                case "+":
                    kq = so1 + so2;
                    break;
                case "-":
                    kq = so1 - so2;
                    break;
                case "*":
                    kq = so1 * so2;
                    break;
                case"/":
                    if (so2 == 0)
                        txtKQ.Text = "ERROR!";
                    if (so2 != 0)
                        kq = so1 / so2;
                    break;                 
            }
        }
        private void bntSo_Click(object sender, EventArgs e)
        {
            Button mySo = (Button)sender;
            string so = mySo.Text;
            if (kq != 0 && trangthainhapso == "dang nhap so1")
            {
                txtKQ.Text = "0";
                trangthainhapso = "cho nhap so1";
            }
            if (trangthainhapso == "cho nhap so1" || trangthainhapso == "cho nhap so2")
            {
                if (so != "0")
                {
                    txtKQ.Text = so;
                    doitrangthai();
                }
                else
                {
                    txtKQ.Text = "0";
                }
            }
            else
            {
                txtKQ.Text += so;
            }
        }

        private void btnDau_Click(object sender, EventArgs e)
        {
            Button myDau = (Button)sender;
            dauphay = false;
            trangthainhapdau = "dang nhap dau";
            if (trangthainhapso == "cho nhap so2" && trangthainhapdau == "dang nhap dau")
            {
                return;
            }
            else
            {
                if (trangthainhapso == "cho nhap so1")
                    trangthainhapso = "dang nhap so1";

                if (trangthainhapso == "dang nhap so2")
                {
                    so2 = double.Parse(txtKQ.Text);
                    tinhkq();
                    so1 = kq;
                    txtKQ.Text = kq.ToString();
                    trangthainhapso = "cho nhap so2";
                }
                pheptoan = myDau.Text;
                if (trangthainhapso == "dang nhap so1")
                {
                    so1 = double.Parse(txtKQ.Text);
                    doitrangthai();
                }
            }
        }

        private void btnBang_Click(object sender, EventArgs e)
        {
            dauphay = false;
            /*if (trangthainhapso == "cho nhap so1" || trangthainhapso == "dang nhap so1")
            {
                so1 = double.Parse(txtKQ.Text);
                trangthainhapso = "dang nhap so1";
            }
            else
            {
                if (trangthainhapdau == "dang nhap dau" && trangthainhapso == "dang nhap so1")
                {
                    trangthainhapso = "dang nhap so2";
                    so2 = so1;
                    tinhkq();
                    so1 = kq;
                    txtKQ.Text = kq.ToString();
                }
                else 
                {
                    if (trangthainhapso == "cho nhap so2")
                    {
                        if (so2.Equals(double.NaN))
                        {
                            so2 = so1;
                        }
                        tinhkq();
                        so1 = kq;
                        trangthainhapso = "dang nhap so1";
                        txtKQ.Text = kq.ToString();
                    }
                    else
                    {
                        if (trangthainhapso == "dang nhap so2" && trangthainhapdau == "dang nhap dau")
                        {
                            if (so2.Equals(double.NaN))
                            {
                                so2 = double.Parse(txtKQ.Text);
                                tinhkq();
                                so1 = kq;
                                txtKQ.Text = kq.ToString();
                                trangthainhapso = "dang nhap so1";
                            }
                            else
                            {
                                tinhkq();
                                so1 = kq;
                                txtKQ.Text = kq.ToString();
                            }

                        }
                    }
                }
            }*/


        }

        private void btnCE_Click(object sender, EventArgs e)
        {
            txtKQ.Text = "0";
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            xoa();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            txtKQ.Text.Remove(txtKQ.Text.Length - 1);
        }

        private void btnPhay_Click(object sender, EventArgs e)
        {
            if (dauphay == false)
            {
                txtKQ.Text += ".";
            }
            dauphay = true;
        }
        /*
        void DoiTrangThai()
        {
            switch (trangthai)
            {
                case "cho nhap so1":
                    trangthai = "dang nhap so1";
                    break;
                case "cho nhap so2":
                    trangthai = "dang nhap so2";
                    break;
                case "dang nhap so1":
                    trangthai = "cho nhap so2";
                    dauphay = false;
                    break;
            }

        }

        void xoa()
        {
            so1 = 0;
            so2 = 0;
            kq = 0;
            xetdau = false;
            dauphay = false;
            trangthai = "cho nhap so1";
            txtKQ.Text = "0";
        }

        void tinhKQ()
        {
            switch (pheptoan)
            {
                case "+": kq = so1 + so2;
                    break;
                case "-": kq = so1 - so2;
                    break;
                case "*": kq = so1 * so2;
                    break;
                case "/":
                    if (so2 == 0)
                        txtKQ.Text = "ERROR";
                    if (so2 != 0)
                        kq = so1 / so2;
                    break;
            }
        }

        //1 2 3 4 5 6 7 8 9 0
        private void bntSO_Click(object sender, EventArgs e)
        {
            Button mySo = (Button)sender;
            string so = mySo.Text;
            if (trangthai == "cho nhap so1" || trangthai == "cho nhap so2")
            {
                if (so != "0")
                {
                    txtKQ.Text = so;
                    DoiTrangThai();
                }
                else
                {
                    txtKQ.Text = "0";                    
                }
            }
            else
            {
                txtKQ.Text += so;
            }
        }

        //+ _ * /
        private void btnDau_Click(object sender, EventArgs e)
        {
            Button myDau = (Button)sender;
            
            if(trangthai=="cho nhap so1")
                trangthai="dang nhap so1";
            if (trangthai == "cho nhap so2")
                trangthai = "dang nhap so2";
            
            if (trangthai == "dang nhap so2")
            {
                
                so2 = double.Parse(txtKQ.Text);
                tinhKQ();
                so1 = kq;                
                txtKQ.Text = kq.ToString();
                trangthai = "cho nhap so2";
            }
            pheptoan = myDau.Text;
            if (trangthai == "dang nhap so1")
            {
                so1 = double.Parse(txtKQ.Text);
                DoiTrangThai();
            }
        }

        //.
        private void btnPhay_Click(object sender, EventArgs e)
        {

        }

        //C
        private void btnClear_Click(object sender, EventArgs e)
        {
            xoa();
        }

        //CE
        private void btnCE_Click(object sender, EventArgs e)
        {
            if (trangthai == "dang nhap so1")
            {
                so1 = 0;
                txtKQ.Text = "0";
                trangthai = "cho nhap so1";
                xetdau = false;
                dauphay = false;
            }
            if (trangthai == "dang nhap so2")
            {
                so2 = 0;
                txtKQ.Text = "0";
                trangthai = "cho nhap so2";
                dauphay = false;
            }
        }

        private void btnBang_Click(object sender, EventArgs e)
        {
            if (trangthai == "cho nhap so1" || trangthai == "dang nhap so1")
            {
                trangthai = "cho nhap so2";
                so1 = double.Parse(txtKQ.Text);
            }
            if (trangthai == "cho nhap so2")
            {
                if (pheptoan != "")
                {
                    tinhKQ();
                    so1 = kq;
                    txtKQ.Text = kq.ToString();
                }
            }
            if (trangthai == "dang nhap so2")
            {
                so2 = double.Parse(txtKQ.Text);
                tinhKQ();
                so1 = kq;
                txtKQ.Text = kq.ToString();
                trangthai = "cho nhap so2";
            }
        }*/

    }
}
